//
//  main.m
//  CagooCmd
//
//  Created by brice rouanet on 15/02/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "CagooConfig.h"

NSString *replaceString(NSString *str)
{
	//replace $LOGNAME by username
	char finalPath[1024]="";
	NSLog(@"aa\n");
	NSArray *separatedString = [str componentsSeparatedByString:@"/"];
	NSMutableArray *separatedStringMut = [[NSMutableArray alloc] init];
	[separatedStringMut addObjectsFromArray:separatedString];
	for(int i=1; i<[separatedStringMut count]; i++)
	{
		strcat(finalPath,"/");
		if ([[separatedStringMut objectAtIndex:i] isEqualToString:@"$LOGNAME"])
		{
			strcat(finalPath,getenv("USER"));
			[separatedStringMut removeObjectAtIndex:i];
			[separatedStringMut insertObject:NSUserName()  atIndex:i];
		}
		else {
			strcat(finalPath,[[separatedStringMut objectAtIndex:i] UTF8String]);
		}

	}
	return [NSString stringWithUTF8String: finalPath];
}


void createDir(NSString *path)
{
	NSTask *task;
	task = [[NSTask alloc] init];
	[task setLaunchPath: @"/bin/mkdir"];
	
	NSArray *arguments;
	arguments = [NSArray arrayWithObjects: path, nil];
	[task setArguments: arguments];
	
	NSPipe *pipe;
	pipe = [NSPipe pipe];
	[task setStandardOutput: pipe];
	
	NSFileHandle *file;
	file = [pipe fileHandleForReading];
	
	[task launch];
	
	NSData *data;
	data = [file readDataToEndOfFile];
	
	NSString *string;
	string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}

NSString *getGroup()
{
	
	NSTask *task;
	task = [[NSTask alloc] init];
	[task setLaunchPath: @"/usr/bin/id"];
	
	NSArray *arguments;
	arguments = [NSArray arrayWithObjects: @"-gn", nil];
	[task setArguments: arguments];
	
	NSPipe *pipe;
	pipe = [NSPipe pipe];
	[task setStandardOutput: pipe];
	
	NSFileHandle *file;
	file = [pipe fileHandleForReading];
	
	[task launch];
	
	NSData *data;
	data = [file readDataToEndOfFile];
	
	NSString *string;
	string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

	return string;
}

void chownDir(NSString *path)
{
	
	NSTask *task;
	//NSString *groupName = getGroup;
	task = [[NSTask alloc] init];
	[task setLaunchPath: @"/bin/chmod"];
	
	NSArray *arguments;
	arguments = [NSArray arrayWithObjects: @"777",path, nil];
	[task setArguments: arguments];
	
	NSPipe *pipe;
	pipe = [NSPipe pipe];
	[task setStandardOutput: pipe];
	
	NSFileHandle *file;
	file = [pipe fileHandleForReading];
	
	[task launch];
	
	NSData *data;
	data = [file readDataToEndOfFile];
	
	NSString *string;
	string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
	
	
	
	
	

}

int main(int argc, char *argv[])
{
	int row=0;
	int cpt=0;
	NSArray *lines;
	
	//read the conf
	NSLog(@"Launch cagoo cmd\n");
	NSArray *allConfig = [[NSArray alloc]initWithContentsOfFile:@"/Library/Application Support/Cagoo/cagoo.conf"];
	NSMutableArray *tableContent = [[NSMutableArray alloc] init];  
	[tableContent addObjectsFromArray:allConfig];
	[tableContent removeObjectAtIndex:0];
	
	row = [tableContent count];
	NSLog(@"cagoo conf readed\n");

	//mount each shares in the array
	while (cpt < row)
	{
		NSLog(@"in cagoo loop\n");
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		


		lines = [[tableContent objectAtIndex:cpt] componentsSeparatedByString:@"@"];
		NSLog(@"serveur : %@ et mount %@\n",[lines objectAtIndex:0],[lines objectAtIndex:1]);
		
		//check if the text contain /Users/, if no try to create mountpoint
		NSArray *testUsers = [[lines objectAtIndex:1] componentsSeparatedByString:@"/"];
		if ([[testUsers objectAtIndex:1] isEqualToString:@"Users"])
		{
			NSLog(@"User home dir, can't try to create directory\n");
		}
		else
		{
			NSLog(@"create dir %@\n",[lines objectAtIndex:1]);
			createDir([lines objectAtIndex:1]);
			chownDir([lines objectAtIndex:1]);
		}

		
		//on monte les share
		NSTask *task;
		task = [[NSTask alloc] init];
		[task setLaunchPath: @"/sbin/mount_smbfs"];
		
		NSArray *arguments;
		arguments = [NSArray arrayWithObjects: replaceString([lines objectAtIndex:0]), replaceString([lines objectAtIndex:1]), nil];
		[task setArguments: arguments];
		
		NSPipe *pipe;
		pipe = [NSPipe pipe];
		[task setStandardOutput: pipe];
		
		NSFileHandle *file;
		file = [pipe fileHandleForReading];
		
		[task launch];
		
		NSData *data;
		data = [file readDataToEndOfFile];
		
		NSString *string;
		string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
		NSLog (@"cagoo mount_smbfs returned :\n%@", string);
		
		
		[pool release];
		
		cpt++;
		
	}


}
